const app = getApp();
const {upTime} = require('/util/public');
Page({
  data: {
    config: app.config,
    
    list:[],
    promoteId: '',
    promoteUserId: '',

    status: -1,

    // 金额
    notSettleAmount: 0,
    orderSumCount: 0,
    promoteUserBalance: 0,
    todayOrderCount: 0,
    todaySettleSumAmount: 0,

    todayTimer: '',

    showOrderType: false,
    typeName: '所有订单'

  },
  onLoad() {
    this.getToday();
    this.getPromoteId();
  },


  // popup
  selectType(){
    this.setData({showOrderType: true});
  },
  onPopupClose(){
      this.setData({showOrderType: false});
  },

  setType(e){
    console.log(555,e);
    let state = e.target.dataset.state;
    if(state.sta == -500){
      this.onPopupClose();
    }else{
      this.setData({
        status: state.sta,
        list: [],
        typeName: state.name
      })
      this.onPopupClose();
      this.getList();
    }
  },


  // 获取推广员id
  getPromoteId(){
    let user = my.getStorageSync({ key: 'user' });
    if (user.data) {
        console.log("user", user)
        this.setData({
          promoteId: user.data.promoteId,
          promoteUserId: user.data.promoteUserId,
        })
        this.getMoneyNum(user.data.promoteUserId);
        this.getList();
    } else {
        this.setData({
          promoteId: '',
          promoteUserId: '',
        })
    }
  },

  // 获取今天时间
  getToday(){
    let timer = new Date();
    let todayTimer = upTime("%Y.%M.%D",timer);
    this.setData({
      todayTimer: todayTimer
    })
  },

  // 获取金额
  getMoneyNum(){
    if(!this.data.promoteUserId){
      my.showToast({
        content:'未获取到推广员信息'
      })
      return;
    }
    let dat = {
      promoteUserId: this.data.promoteUserId,
    }
    app.user.statistics(dat)
    .then(res=>{
      console.log(222,res);
      this.setData({
        notSettleAmount: res.data.notSettleAmount,
        orderSumCount: res.data.orderSumCount,
        promoteUserBalance: res.data.promoteUserBalance,
        todayOrderCount: res.data.todayOrderCount,
        todaySettleSumAmount: res.data.todaySettleSumAmount,
      })
    })
  },


  // 获取订单列表
  getList(){
    if(!this.data.promoteUserId){
      my.showToast({
        content:'未获取到推广员信息'
      })
      return;
    }
    let dat = {
      promoteUserId: this.data.promoteUserId,
      // promoteUserId: 72,
      status: this.data.status,
      page: 1,
      rows: 10
    }

    my.showLoading({
      content: '加载中...',
    });

    app.user.pageByPromoteUserId(dat)
    .then(res=>{
      res.data.list.forEach(item=>{
        item.createTimeStrFront = upTime("%Y.%M.%D",item.createtime);
      })
      this.setData({
        list: res.data.list
      })
      my.hideLoading();
    })
    .catch(err=>{
      my.hideLoading();
    })

  },








  // 跳转
  navigateTo(e){
    app.navigateTo(e)
  },



});
